package org.james.deleter.task;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.james.deleter.components.InfoListBox;
import org.james.deleter.utils.FileOperater;
import org.james.deleter.utils.FileUtils;

import application.Main;
import javafx.application.Platform;
import javafx.concurrent.Task;

public class FileHandleTask extends Task<String> {

	private String type = null;
	private String folder = null;
	private String extensions = null;
	private boolean isDeleteRecur = true;
	
	public FileHandleTask(String type,String folder,String extensions,boolean isDeleteRecur) {
		this.type = type;
		this.folder = folder;
		this.extensions = extensions;
		this.isDeleteRecur = isDeleteRecur;
	}
	
	public FileHandleTask(String type,String folder) {
		this.type = type;
		this.folder = folder;
	}

	@Override
	protected String call() throws Exception {
		if(StringUtils.equals("1", type)){
			FileOperater.deleteFileByExtensions(folder, extensions,isDeleteRecur);
		}else if(StringUtils.equals("2", type)){
			FileOperater.deleteFilesByKeywords(folder, extensions,isDeleteRecur);
		}else if(StringUtils.equals("3", type)){
			if(StringUtils.isNotBlank(folder)){
				File f = new File(folder);
				FileUtils.forceDelete(f);
				InfoListBox.addMessageInfo("删除文件夹成功："+folder);
				Platform.runLater(()-> {
					Main.execBtn.setText("确定");
					Main.execBtn.setDisable(false);
				});
			}
		}
		return null;
	}

}
